home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb / sprite / RCS / values.c,v < prev   
Encoding:
Text File  |  1990-12-10  |  27.6 KB  |  1,091 lines

  1. head     1.2;
  2. branch   ;
  3. access   ;
  4. symbols  ;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.2
  10. date     90.12.09.22.36.15;  author rab;  state Exp;
  11. branches ;
  12. next     1.1;
  13.  
  14. 1.1
  15. date     90.11.16.14.49.17;  author rab;  state Exp;
  16. branches ;
  17. next     ;
  18.  
  19.  
  20. desc
  21. @@
  22.  
  23.  
  24. 1.2
  25. log
  26. @Changes for Sprite. (Mike checking in for Bob.)
  27. @
  28. text
  29. @/* Low level packing and unpacking of values for GDB.
  30.    Copyright (C) 1986, 1987, 1989 Free Software Foundation, Inc.
  31.  
  32. This file is part of GDB.
  33.  
  34. GDB is free software; you can redistribute it and/or modify
  35. it under the terms of the GNU General Public License as published by
  36. the Free Software Foundation; either version 1, or (at your option)
  37. any later version.
  38.  
  39. GDB is distributed in the hope that it will be useful,
  40. but WITHOUT ANY WARRANTY; without even the implied warranty of
  41. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  42. GNU General Public License for more details.
  43.  
  44. You should have received a copy of the GNU General Public License
  45. along with GDB; see the file COPYING.  If not, write to
  46. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  47.  
  48. #include <stdio.h>
  49. #include "defs.h"
  50. #include "param.h"
  51. #include "symtab.h"
  52. #include "value.h"
  53.  
  54. /* The value-history records all the values printed
  55.    by print commands during this session.  Each chunk
  56.    records 60 consecutive values.  The first chunk on
  57.    the chain records the most recent values.
  58.    The total number of values is in value_history_count.  */
  59.  
  60. #define VALUE_HISTORY_CHUNK 60
  61.  
  62. struct value_history_chunk
  63. {
  64.   struct value_history_chunk *next;
  65.   value values[VALUE_HISTORY_CHUNK];
  66. };
  67.  
  68. /* Chain of chunks now in use.  */
  69.  
  70. static struct value_history_chunk *value_history_chain;
  71.  
  72. static int value_history_count;    /* Abs number of last entry stored */
  73.  
  74.  
  75. /* List of all value objects currently allocated
  76.    (except for those released by calls to release_value)
  77.    This is so they can be freed after each command.  */
  78.  
  79. static value all_values;
  80.  
  81. /* Allocate a  value  that has the correct length for type TYPE.  */
  82.  
  83. value
  84. allocate_value (type)
  85.      struct type *type;
  86. {
  87.   register value val;
  88.  
  89.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type));
  90.   VALUE_NEXT (val) = all_values;
  91.   all_values = val;
  92.   VALUE_TYPE (val) = type;
  93.   VALUE_LVAL (val) = not_lval;
  94.   VALUE_ADDRESS (val) = 0;
  95.   VALUE_FRAME (val) = 0;
  96.   VALUE_OFFSET (val) = 0;
  97.   VALUE_BITPOS (val) = 0;
  98.   VALUE_BITSIZE (val) = 0;
  99.   VALUE_REPEATED (val) = 0;
  100.   VALUE_REPETITIONS (val) = 0;
  101.   VALUE_REGNO (val) = -1;
  102.   return val;
  103. }
  104.  
  105. /* Allocate a  value  that has the correct length
  106.    for COUNT repetitions type TYPE.  */
  107.  
  108. value
  109. allocate_repeat_value (type, count)
  110.      struct type *type;
  111.      int count;
  112. {
  113.   register value val;
  114.  
  115.   val = (value) xmalloc (sizeof (struct value) + TYPE_LENGTH (type) * count);
  116.   VALUE_NEXT (val) = all_values;
  117.   all_values = val;
  118.   VALUE_TYPE (val) = type;
  119.   VALUE_LVAL (val) = not_lval;
  120.   VALUE_ADDRESS (val) = 0;
  121.   VALUE_FRAME (val) = 0;
  122.   VALUE_OFFSET (val) = 0;
  123.   VALUE_BITPOS (val) = 0;
  124.   VALUE_BITSIZE (val) = 0;
  125.   VALUE_REPEATED (val) = 1;
  126.   VALUE_REPETITIONS (val) = count;
  127.   VALUE_REGNO (val) = -1;
  128.   return val;
  129. }
  130.  
  131. /* Free all the values that have been allocated (except for those released).
  132.    Called after each command, successful or not.  */
  133.  
  134. void
  135. free_all_values ()
  136. {
  137.   register value val, next;
  138.  
  139.   for (val = all_values; val; val = next)
  140.     {
  141.       next = VALUE_NEXT (val);
  142.       free (val);
  143.     }
  144.  
  145.   all_values = 0;
  146. }
  147.  
  148. /* Remove VAL from the chain all_values
  149.    so it will not be freed automatically.  */
  150.  
  151. void
  152. release_value (val)
  153.      register value val;
  154. {
  155.   register value v;
  156.  
  157.   if (all_values == val)
  158.     {
  159.       all_values = val->next;
  160.       return;
  161.     }
  162.  
  163.   for (v = all_values; v; v = v->next)
  164.     {
  165.       if (v->next == val)
  166.     {
  167.       v->next = val->next;
  168.       break;
  169.     }
  170.     }
  171. }
  172.  
  173. /* Return a copy of the value ARG.
  174.    It contains the same contents, for same memory address,
  175.    but it's a different block of storage.  */
  176.  
  177. static value
  178. value_copy (arg)
  179.      value arg;
  180. {
  181.   register value val;
  182.   register struct type *type = VALUE_TYPE (arg);
  183.   if (VALUE_REPEATED (arg))
  184.     val = allocate_repeat_value (type, VALUE_REPETITIONS (arg));
  185.   else
  186.     val = allocate_value (type);
  187.   VALUE_LVAL (val) = VALUE_LVAL (arg);
  188.   VALUE_ADDRESS (val) = VALUE_ADDRESS (arg);
  189.   VALUE_OFFSET (val) = VALUE_OFFSET (arg);
  190.   VALUE_BITPOS (val) = VALUE_BITPOS (arg);
  191.   VALUE_BITSIZE (val) = VALUE_BITSIZE (arg);
  192.   VALUE_REGNO (val) = VALUE_REGNO (arg);
  193.   bcopy (VALUE_CONTENTS (arg), VALUE_CONTENTS (val),
  194.      TYPE_LENGTH (VALUE_TYPE (arg))
  195.      * (VALUE_REPEATED (arg) ? VALUE_REPETITIONS (arg) : 1));
  196.   return val;
  197. }
  198.  
  199. /* Access to the value history.  */
  200.  
  201. /* Record a new value in the value history.
  202.    Returns the absolute history index of the entry.  */
  203.  
  204. int
  205. record_latest_value (val)
  206.      value val;
  207. {
  208.   int i;
  209.   double foo;
  210.  
  211.   /* Check error now if about to store an invalid float.  We return -1
  212.      to the caller, but allow them to continue, e.g. to print it as "Nan". */
  213.   if (TYPE_CODE (VALUE_TYPE (val)) == TYPE_CODE_FLT) {
  214.     foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &i);
  215.     if (i) return -1;        /* Indicate value not saved in history */
  216.   }
  217.  
  218.   /* Here we treat value_history_count as origin-zero
  219.      and applying to the value being stored now.  */
  220.  
  221.   i = value_history_count % VALUE_HISTORY_CHUNK;
  222.   if (i == 0)
  223.     {
  224.       register struct value_history_chunk *new
  225.     = (struct value_history_chunk *)
  226.       xmalloc (sizeof (struct value_history_chunk));
  227.       bzero (new->values, sizeof new->values);
  228.       new->next = value_history_chain;
  229.       value_history_chain = new;
  230.     }
  231.  
  232.   value_history_chain->values[i] = val;
  233.   release_value (val);
  234.  
  235.   /* Now we regard value_history_count as origin-one
  236.      and applying to the value just stored.  */
  237.  
  238.   return ++value_history_count;
  239. }
  240.  
  241. /* Return a copy of the value in the history with sequence number NUM.  */
  242.  
  243. value
  244. access_value_history (num)
  245.      int num;
  246. {
  247.   register struct value_history_chunk *chunk;
  248.   register int i;
  249.   register int absnum = num;
  250.  
  251.   if (absnum <= 0)
  252.     absnum += value_history_count;
  253.  
  254.   if (absnum <= 0)
  255.     {
  256.       if (num == 0)
  257.     error ("The history is empty.");
  258.       else if (num == 1)
  259.     error ("There is only one value in the history.");
  260.       else
  261.     error ("History does not go back to $$%d.", -num);
  262.     }
  263.   if (absnum > value_history_count)
  264.     error ("History has not yet reached $%d.", absnum);
  265.  
  266.   absnum--;
  267.  
  268.   /* Now absnum is always absolute and origin zero.  */
  269.  
  270.   chunk = value_history_chain;
  271.   for (i = (value_history_count - 1) / VALUE_HISTORY_CHUNK - absnum / VALUE_HISTORY_CHUNK;
  272.        i > 0; i--)
  273.     chunk = chunk->next;
  274.  
  275.   return value_copy (chunk->values[absnum % VALUE_HISTORY_CHUNK]);
  276. }
  277.  
  278. /* Clear the value history entirely.
  279.    Must be done when new symbol tables are loaded,
  280.    because the type pointers become invalid.  */
  281.  
  282. void
  283. clear_value_history ()
  284. {
  285.   register struct value_history_chunk *next;
  286.   register int i;
  287.   register value val;
  288.  
  289.   while (value_history_chain)
  290.     {
  291.       for (i = 0; i < VALUE_HISTORY_CHUNK; i++)
  292.     if (val = value_history_chain->values[i])
  293.       free (val);
  294.       next = value_history_chain->next;
  295.       free (value_history_chain);
  296.       value_history_chain = next;
  297.     }
  298.   value_history_count = 0;
  299. }
  300.  
  301. static void
  302. value_history_info (num_exp, from_tty)
  303.      char *num_exp;
  304.      int from_tty;
  305. {
  306.   register int i;
  307.   register value val;
  308.   static int num = 1;
  309.  
  310.   if (num_exp)
  311.     {
  312.       if (num_exp[0] == '+' && num_exp[1] == '\0')
  313.     /* "info history +" should print from the stored position.  */
  314.     ;
  315.       else
  316.     /* "info history <exp>" should print around value number <exp>.  */
  317.     num = parse_and_eval_address (num_exp) - 5;
  318.     }
  319.   else
  320.     {
  321.       /* "info history" means print the last 10 values.  */
  322.       num = value_history_count - 9;
  323.     }
  324.  
  325.   if (num <= 0)
  326.     num = 1;
  327.  
  328.   for (i = num; i < num + 10 && i <= value_history_count; i++)
  329.     {
  330.       val = access_value_history (i);
  331.       printf_filtered ("$%d = ", i);
  332.       value_print (val, stdout, 0, Val_pretty_default);
  333.       printf_filtered ("\n");
  334.     }
  335.  
  336.   /* The next "info history +" should start after what we just printed.  */
  337.   num += 10;
  338.  
  339.   /* Hitting just return after this command should do the same thing as
  340.      "info history +".  If num_exp is null, this is unnecessary, since
  341.      "info history +" is not useful after "info history".  */
  342.   if (from_tty && num_exp)
  343.     {
  344.       num_exp[0] = '+';
  345.       num_exp[1] = '\0';
  346.     }
  347. }
  348.  
  349. /* Internal variables.  These are variables within the debugger
  350.    that hold values assigned by debugger commands.
  351.    The user refers to them with a '$' prefix
  352.    that does not appear in the variable names stored internally.  */
  353.  
  354. static struct internalvar *internalvars;
  355.  
  356. /* Look up an internal variable with name NAME.  NAME should not
  357.    normally include a dollar sign.
  358.  
  359.    If the specified internal variable does not exist,
  360.    one is created, with a void value.  */
  361.  
  362. struct internalvar *
  363. lookup_internalvar (name)
  364.      char *name;
  365. {
  366.   register struct internalvar *var;
  367.  
  368.   for (var = internalvars; var; var = var->next)
  369.     if (!strcmp (var->name, name))
  370.       return var;
  371.  
  372.   var = (struct internalvar *) xmalloc (sizeof (struct internalvar));
  373.   var->name = concat (name, "", "");
  374.   var->value = allocate_value (builtin_type_void);
  375.   release_value (var->value);
  376.   var->next = internalvars;
  377.   internalvars = var;
  378.   return var;
  379. }
  380.  
  381. value
  382. value_of_internalvar (var)
  383.      struct internalvar *var;
  384. {
  385.   register value val;
  386.  
  387. #ifdef IS_TRAPPED_INTERNALVAR
  388.   if (IS_TRAPPED_INTERNALVAR (var->name))
  389.     return VALUE_OF_TRAPPED_INTERNALVAR (var);
  390. #endif 
  391.  
  392.   val = value_copy (var->value);
  393.   VALUE_LVAL (val) = lval_internalvar;
  394.   VALUE_INTERNALVAR (val) = var;
  395.   return val;
  396. }
  397.  
  398. void
  399. set_internalvar_component (var, offset, bitpos, bitsize, newval)
  400.      struct internalvar *var;
  401.      int offset, bitpos, bitsize;
  402.      value newval;
  403. {
  404.   register char *addr = VALUE_CONTENTS (var->value) + offset;
  405.  
  406. #ifdef IS_TRAPPED_INTERNALVAR
  407.   if (IS_TRAPPED_INTERNALVAR (var->name))
  408.     SET_TRAPPED_INTERNALVAR (var, newval, bitpos, bitsize, offset);
  409. #endif
  410.  
  411.   if (bitsize)
  412.     modify_field (addr, (int) value_as_long (newval),
  413.           bitpos, bitsize);
  414.   else
  415.     bcopy (VALUE_CONTENTS (newval), addr,
  416.        TYPE_LENGTH (VALUE_TYPE (newval)));
  417. }
  418.  
  419. void
  420. set_internalvar (var, val)
  421.      struct internalvar *var;
  422.      value val;
  423. {
  424. #ifdef IS_TRAPPED_INTERNALVAR
  425.   if (IS_TRAPPED_INTERNALVAR (var->name))
  426.     SET_TRAPPED_INTERNALVAR (var, val, 0, 0, 0);
  427. #endif
  428.  
  429.   free (var->value);
  430.   var->value = value_copy (val);
  431.   release_value (var->value);
  432. }
  433.  
  434. char *
  435. internalvar_name (var)
  436.      struct internalvar *var;
  437. {
  438.   return var->name;
  439. }
  440.  
  441. /* Free all internalvars.  Done when new symtabs are loaded,
  442.    because that makes the values invalid.  */
  443.  
  444. void
  445. clear_internalvars ()
  446. {
  447.   register struct internalvar *var;
  448.  
  449.   while (internalvars)
  450.     {
  451.       var = internalvars;
  452.       internalvars = var->next;
  453.       free (var->name);
  454.       free (var->value);
  455.       free (var);
  456.     }
  457. }
  458.  
  459. static void
  460. convenience_info ()
  461. {
  462.   register struct internalvar *var;
  463.   int varseen = 0;
  464.  
  465.   for (var = internalvars; var; var = var->next)
  466.     {
  467. #ifdef IS_TRAPPED_INTERNALVAR
  468.       if (IS_TRAPPED_INTERNALVAR (var->name))
  469.     continue;
  470. #endif
  471.       if (!varseen)
  472.     {
  473.       printf ("Debugger convenience variables:\n\n");
  474.       varseen = 1;
  475.     }
  476.       printf ("$%s: ", var->name);
  477.       value_print (var->value, stdout, 0, Val_pretty_default);
  478.       printf ("\n");
  479.     }
  480.   if (!varseen)
  481.     printf ("No debugger convenience variables now defined.\n\
  482. Convenience variables have names starting with \"$\";\n\
  483. use \"set\" as in \"set $foo = 5\" to define them.\n");
  484. }
  485.  
  486. /* Extract a value as a C number (either long or double).
  487.    Knows how to convert fixed values to double, or
  488.    floating values to long.
  489.    Does not deallocate the value.  */
  490.  
  491. LONGEST
  492. value_as_long (val)
  493.      register value val;
  494. {
  495.   return unpack_long (VALUE_TYPE (val), VALUE_CONTENTS (val));
  496. }
  497.  
  498. double
  499. value_as_double (val)
  500.      register value val;
  501. {
  502.   double foo;
  503.   int inv;
  504.   
  505.   foo = unpack_double (VALUE_TYPE (val), VALUE_CONTENTS (val), &inv);
  506.   if (inv)
  507.     error ("Invalid floating value found in program.");
  508.   return foo;
  509. }
  510.  
  511. /* Unpack raw data (copied from debugee) at VALADDR
  512.    as a long, or as a double, assuming the raw data is described
  513.    by type TYPE.  Knows how to convert different sizes of values
  514.    and can convert between fixed and floating point.
  515.  
  516.    C++: It is assumed that the front-end has taken care of
  517.    all matters concerning pointers to members.  A pointer
  518.    to member which reaches here is considered to be equivalent
  519.    to an INT (or some size).  After all, it is only an offset.  */
  520.  
  521. /*
  522.  * To allow cross machine type debugging in Sprite we bcopy the 
  523.  * data before returning it.  This is done so we can debugger sun3
  524.  * from sun4 that have stricter alignment constraints.
  525.  */
  526.  
  527. LONGEST
  528. unpack_long (type, valaddr)
  529.      struct type *type;
  530.      char *valaddr;
  531. {
  532.   register enum type_code code = TYPE_CODE (type);
  533.   register int len = TYPE_LENGTH (type);
  534.   register int nosign = TYPE_UNSIGNED (type);
  535. #ifdef KGDB
  536.   union { 
  537.    double    foo; /* double forces alignment. */
  538.    char    buffer[8];
  539.   } tmpMem;
  540.   bcopy(valaddr,tmpMem.buffer,8);
  541.   valaddr = tmpMem.buffer;
  542. #endif
  543.  
  544.   if (code == TYPE_CODE_ENUM)
  545.     code = TYPE_CODE_INT;
  546.   if (code == TYPE_CODE_FLT)
  547.     {
  548.       if (len == sizeof (float))
  549.     return * (float *) valaddr;
  550.  
  551.       if (len == sizeof (double))
  552.     return * (double *) valaddr;
  553.     }
  554.   else if (code == TYPE_CODE_INT && nosign)
  555.     {
  556.       if (len == sizeof (char))
  557.     return * (unsigned char *) valaddr;
  558.  
  559.       if (len == sizeof (short))
  560.     return * (unsigned short *) valaddr;
  561.  
  562.       if (len == sizeof (int))
  563.     return * (unsigned int *) valaddr;
  564.  
  565.       if (len == sizeof (long))
  566.     return * (unsigned long *) valaddr;
  567. #ifdef LONG_LONG
  568.       if (len == sizeof (long long))
  569.     return * (unsigned long long *) valaddr;
  570. #endif
  571.     }
  572.   else if (code == TYPE_CODE_INT)
  573.     {
  574.       if (len == sizeof (char))
  575.     return * (char *) valaddr;
  576.  
  577.       if (len == sizeof (short))
  578.     return * (short *) valaddr;
  579.  
  580.       if (len == sizeof (int))
  581.     return * (int *) valaddr;
  582.  
  583.       if (len == sizeof (long))
  584.     return * (long *) valaddr;
  585.  
  586. #ifdef LONG_LONG
  587.       if (len == sizeof (long long))
  588.     return * (long long *) valaddr;
  589. #endif
  590.     }
  591.   else if (code == TYPE_CODE_PTR
  592.        || code == TYPE_CODE_REF)
  593.     {
  594.       if (len == sizeof (char *))
  595.     return (CORE_ADDR) * (char **) valaddr;
  596.     }
  597.   else if (code == TYPE_CODE_MEMBER)
  598.     error ("not implemented: member types in unpack_long");
  599.  
  600.   error ("Value not integer or pointer.");
  601. }
  602.  
  603. /* Return a double value from the specified type and address.
  604.    INVP points to an int which is set to 0 for valid value,
  605.    1 for invalid value (bad float format).  In either case,
  606.    the returned double is OK to use.  */
  607.  
  608. double
  609. unpack_double (type, valaddr, invp)
  610.      struct type *type;
  611.      char *valaddr;
  612.      int *invp;
  613. {
  614.   register enum type_code code = TYPE_CODE (type);
  615.   register int len = TYPE_LENGTH (type);
  616.   register int nosign = TYPE_UNSIGNED (type);
  617. #ifdef KGDB
  618.   union { 
  619.    double    foo; /* double forces alignment. */
  620.    char    buffer[8];
  621.   } tmpMem;
  622.   bcopy(valaddr,tmpMem.buffer,8);
  623.   valaddr = tmpMem.buffer;
  624. #endif
  625.  
  626.   *invp = 0;            /* Assume valid.   */
  627.   if (code == TYPE_CODE_FLT)
  628.     {
  629.       if (INVALID_FLOAT (valaddr, len))
  630.     {
  631.       *invp = 1;
  632.       return 1.234567891011121314;
  633.     }
  634.  
  635.       if (len == sizeof (float))
  636.     return * (float *) valaddr;
  637.  
  638.       if (len == sizeof (double))
  639.     {
  640.       /* Some machines require doubleword alignment for doubles.
  641.          This code works on them, and on other machines.  */
  642.       double temp;
  643.       bcopy ((char *) valaddr, (char *) &temp, sizeof (double));
  644.       return temp;
  645.     }
  646.     }
  647.   else if (code == TYPE_CODE_INT && nosign)
  648.     {
  649.       if (len == sizeof (char))
  650.     return * (unsigned char *) valaddr;
  651.  
  652.       if (len == sizeof (short))
  653.     return * (unsigned short *) valaddr;
  654.  
  655.       if (len == sizeof (int))
  656.     return * (unsigned int *) valaddr;
  657.  
  658.       if (len == sizeof (long))
  659.     return * (unsigned long *) valaddr;
  660.  
  661. #ifdef LONG_LONG
  662.       if (len == sizeof (long long))
  663.     return * (unsigned long long *) valaddr;
  664. #endif
  665.     }
  666.   else if (code == TYPE_CODE_INT)
  667.     {
  668.       if (len == sizeof (char))
  669.     return * (char *) valaddr;
  670.  
  671.       if (len == sizeof (short))
  672.     return * (short *) valaddr;
  673.  
  674.       if (len == sizeof (int))
  675.     return * (int *) valaddr;
  676.  
  677.       if (len == sizeof (long))
  678.     return * (long *) valaddr;
  679.  
  680. #ifdef LONG_LONG
  681.       if (len == sizeof (long long))
  682.     return * (long long *) valaddr;
  683. #endif
  684.     }
  685.  
  686.   error ("Value not floating number.");
  687.   /* NOTREACHED */
  688.   return (double) 0;        /* To silence compiler warning.  */
  689. }
  690.  
  691. /* Given a value ARG1 of a struct or union type,
  692.    extract and return the value of one of its fields.
  693.    FIELDNO says which field.
  694.  
  695.    For C++, must also be able to return values from static fields */
  696.  
  697. value
  698. value_field (arg1, fieldno)
  699.      register value arg1;
  700.      register int fieldno;
  701. {
  702.   register value v;
  703.   register struct type *type = TYPE_FIELD_TYPE (VALUE_TYPE (arg1), fieldno);
  704.   register int offset;
  705.  
  706.   /* Handle packed fields */
  707.  
  708.   offset = TYPE_FIELD_BITPOS (VALUE_TYPE (arg1), fieldno) / 8;
  709.   if (TYPE_FIELD_BITSIZE (VALUE_TYPE (arg1), fieldno))
  710.     {
  711.       v = value_from_long (type,
  712.                unpack_field_as_long (VALUE_TYPE (arg1),
  713.                          VALUE_CONTENTS (arg1),
  714.                          fieldno));
  715.       VALUE_BITPOS (v) = TYPE_FIELD_BITPOS (VALUE_TYPE (arg1), fieldno) % 8;
  716.       VALUE_BITSIZE (v) = TYPE_FIELD_BITSIZE (VALUE_TYPE (arg1), fieldno);
  717.     }
  718.   else
  719.     {
  720.       v = allocate_value (type);
  721.       bcopy (VALUE_CONTENTS (arg1) + offset,
  722.          VALUE_CONTENTS (v),
  723.          TYPE_LENGTH (type));
  724.     }
  725.   VALUE_LVAL (v) = VALUE_LVAL (arg1);
  726.   if (VALUE_LVAL (arg1) == lval_internalvar)
  727.     VALUE_LVAL (v) = lval_internalvar_component;
  728.   VALUE_ADDRESS (v) = VALUE_ADDRESS (arg1);
  729.   VALUE_OFFSET (v) = offset + VALUE_OFFSET (arg1);
  730.   return v;
  731. }
  732.  
  733. value
  734. value_fn_field (arg1, fieldno, subfieldno)
  735.      register value arg1;
  736.      register int fieldno;
  737. {
  738.   register value v;
  739.   struct fn_field *f = TYPE_FN_FIELDLIST1 (VALUE_TYPE (arg1), fieldno);
  740.   register struct type *type = TYPE_FN_FIELD_TYPE (f, subfieldno);
  741.   struct symbol *sym;
  742.  
  743.   sym = lookup_symbol (TYPE_FN_FIELD_PHYSNAME (f, subfieldno),
  744.                0, VAR_NAMESPACE, 0);
  745.   if (! sym) error ("Internal error: could not find physical method named %s",
  746.             TYPE_FN_FIELD_PHYSNAME (f, subfieldno));
  747.   
  748.   v = allocate_value (type);
  749.   VALUE_ADDRESS (v) = BLOCK_START (SYMBOL_BLOCK_VALUE (sym));
  750.   VALUE_TYPE (v) = type;
  751.   return v;
  752. }
  753.  
  754. /* Return a virtual function as a value.
  755.    ARG1 is the object which provides the virtual function
  756.    table pointer.
  757.    F is the list of member functions which contains the desired virtual
  758.    function.
  759.    J is an index into F which provides the desired virtual function.
  760.    TYPE is the basetype which first provides the virtual function table.  */
  761. value
  762. value_virtual_fn_field (arg1, f, j, type)
  763.      value arg1;
  764.      struct fn_field *f;
  765.      int j;
  766.      struct type *type;
  767. {
  768.   /* First, get the virtual function table pointer.  That comes
  769.      with a strange type, so cast it to type `pointer to long' (which
  770.      should serve just fine as a function type).  Then, index into
  771.      the table, and convert final value to appropriate function type.  */
  772.   value vfn, vtbl;
  773.   value vi = value_from_long (builtin_type_int, 
  774.                   (LONGEST) TYPE_FN_FIELD_VOFFSET (f, j));
  775.   VALUE_TYPE (arg1) = TYPE_VPTR_BASETYPE (type);
  776.  
  777.   /* This type may have been defined before its virtual function table
  778.      was.  If so, fill in the virtual function table entry for the
  779.      type now.  */
  780.   if (TYPE_VPTR_FIELDNO (type) < 0)
  781.     TYPE_VPTR_FIELDNO (type)
  782.       = fill_in_vptr_fieldno (type);
  783.  
  784.   /* The virtual function table is now an array of structures
  785.      which have the form { int16 offset, delta; void *pfn; }.  */
  786.   vtbl = value_ind (value_field (arg1, TYPE_VPTR_FIELDNO (type)));
  787.  
  788.   /* Index into the virtual function table.  This is hard-coded because
  789.      looking up a field is not cheap, and it may be important to save
  790.      time, e.g. if the user has set a conditional breakpoint calling
  791.      a virtual function.  */
  792.   vfn = value_field (value_subscript (vtbl, vi), 2);
  793.  
  794.   /* Reinstantiate the function pointer with the correct type.  */
  795.   VALUE_TYPE (vfn) = lookup_pointer_type (TYPE_FN_FIELD_TYPE (f, j));
  796.   return vfn;
  797. }
  798.  
  799. /* The value of a static class member does not depend
  800.    on its instance, only on its type.  If FIELDNO >= 0,
  801.    then fieldno is a valid field number and is used directly.
  802.    Otherwise, FIELDNAME is the name of the field we are
  803.    searching for.  If it is not a static field name, an
  804.    error is signaled.  TYPE is the type in which we look for the
  805.    static field member.  */
  806. value
  807. value_static_field (type, fieldname, fieldno)
  808.      register struct type *type;
  809.      char *fieldname;
  810.      register int fieldno;
  811. {
  812.   register value v;
  813.   struct symbol *sym;
  814.  
  815.   if (fieldno < 0)
  816.     {
  817.       register struct type *t = type;
  818.       /* Look for static field.  */
  819.       while (t)
  820.     {
  821.       int i;
  822.       for (i = TYPE_NFIELDS (t) - 1; i >= 0; i--)
  823.         if (! strcmp (TYPE_FIELD_NAME (t, i), fieldname))
  824.           {
  825.         if (TYPE_FIELD_STATIC (t, i))
  826.           {
  827.             fieldno = i;
  828.             goto found;
  829.           }
  830.         else
  831.           error ("field `%s' is not static");
  832.           }
  833.       t = TYPE_BASECLASSES (t) ? TYPE_BASECLASS (t, 1) : 0;
  834.     }
  835.  
  836.       t = type;
  837.  
  838.       if (destructor_name_p (fieldname, t))
  839.     error ("use `info method' command to print out value of destructor");
  840.  
  841.       while (t)
  842.     {
  843.       int i, j;
  844.  
  845.       for (i = TYPE_NFN_FIELDS (t) - 1; i >= 0; i--)
  846.         {
  847.           if (! strcmp (TYPE_FN_FIELDLIST_NAME (t, i), fieldname))
  848.         {
  849.           error ("use `info method' command to print value of method \"%s\"", fieldname);
  850.         }
  851.         }
  852.       t = TYPE_BASECLASSES (t) ? TYPE_BASECLASS (t, 1) : 0;
  853.     }
  854.       error("there is no field named %s", fieldname);
  855.     }
  856.  
  857.  found:
  858.  
  859.   sym = lookup_symbol (TYPE_FIELD_STATIC_PHYSNAME (type, fieldno),
  860.                0, VAR_NAMESPACE, 0);
  861.   if (! sym) error ("Internal error: could not find physical static variable named %s", TYPE_FIELD_BITSIZE (type, fieldno));
  862.  
  863.   type = TYPE_FIELD_TYPE (type, fieldno);
  864.   v = value_at (type, (CORE_ADDR)SYMBOL_BLOCK_VALUE (sym));
  865.   return v;
  866. }
  867.  
  868. long
  869. unpack_field_as_long (type, valaddr, fieldno)
  870.      struct type *type;
  871.      char *valaddr;
  872.      int fieldno;
  873. {
  874.   long val;
  875.   int bitpos = TYPE_FIELD_BITPOS (type, fieldno);
  876.   int bitsize = TYPE_FIELD_BITSIZE (type, fieldno);
  877.  
  878.   bcopy (valaddr + bitpos / 8, &val, sizeof val);
  879.  
  880.   /* Extracting bits depends on endianness of the machine.  */
  881. #ifdef BITS_BIG_ENDIAN
  882.   val = val >> (sizeof val * 8 - bitpos % 8 - bitsize);
  883. #else
  884.   val = val >> (bitpos % 8);
  885. #endif
  886.  
  887.   val &= (1 << bitsize) - 1;
  888.   return val;
  889. }
  890.  
  891. void
  892. modify_field (addr, fieldval, bitpos, bitsize)
  893.      char *addr;
  894.      int fieldval;
  895.      int bitpos, bitsize;
  896. {
  897.   long oword;
  898.  
  899.   /* Reject values too big to fit in the field in question.
  900.      Otherwise adjoining fields may be corrupted.  */
  901.   if (fieldval & ~((1<<bitsize)-1))
  902.     error ("Value %d does not fit in %d bits.", fieldval, bitsize);
  903.   
  904.   bcopy (addr, &oword, sizeof oword);
  905.  
  906.   /* Shifting for bit field depends on endianness of the machine.  */
  907. #ifdef BITS_BIG_ENDIAN
  908.   bitpos = sizeof (oword) * 8 - bitpos - bitsize;
  909. #endif
  910.  
  911.   oword &= ~(((1 << bitsize) - 1) << bitpos);
  912.   oword |= fieldval << bitpos;
  913.   bcopy (&oword, addr, sizeof oword);
  914. }
  915.  
  916. /* Convert C numbers into newly allocated values */
  917.  
  918. value
  919. value_from_long (type, num)
  920.      struct type *type;
  921.      register LONGEST num;
  922. {
  923.   register value val = allocate_value (type);
  924.   register enum type_code code = TYPE_CODE (type);
  925.   register int len = TYPE_LENGTH (type);
  926.  
  927.   if (code == TYPE_CODE_INT || code == TYPE_CODE_ENUM)
  928.     {
  929.       if (len == sizeof (char))
  930.     * (char *) VALUE_CONTENTS (val) = num;
  931.       else if (len == sizeof (short))
  932.     * (short *) VALUE_CONTENTS (val) = num;
  933.       else if (len == sizeof (int))
  934.     * (int *) VALUE_CONTENTS (val) = num;
  935.       else if (len == sizeof (long))
  936.     * (long *) VALUE_CONTENTS (val) = num;
  937. #ifdef LONG_LONG
  938.       else if (len == sizeof (long long))
  939.     * (long long *) VALUE_CONTENTS (val) = num;
  940. #endif
  941.       else
  942.     error ("Integer type encountered with unexpected data length.");
  943.     }
  944.   else
  945.     error ("Unexpected type encountered for integer constant.");
  946.  
  947.   return val;
  948. }
  949.  
  950. value
  951. value_from_double (type, num)
  952.      struct type *type;
  953.      double num;
  954. {
  955.   register value val = allocate_value (type);
  956.   register enum type_code code = TYPE_CODE (type);
  957.   register int len = TYPE_LENGTH (type);
  958.  
  959.   if (code == TYPE_CODE_FLT)
  960.     {
  961.       if (len == sizeof (float))
  962.     * (float *) VALUE_CONTENTS (val) = num;
  963.       else if (len == sizeof (double))
  964.     * (double *) VALUE_CONTENTS (val) = num;
  965.       else
  966.     error ("Floating type encountered with unexpected data length.");
  967.     }
  968.   else
  969.     error ("Unexpected type encountered for floating constant.");
  970.  
  971.   return val;
  972. }
  973.  
  974. /* Deal with the value that is "about to be returned".  */
  975.  
  976. /* Return the value that a function returning now
  977.    would be returning to its caller, assuming its type is VALTYPE.
  978.    RETBUF is where we look for what ought to be the contents
  979.    of the registers (in raw form).  This is because it is often
  980.    desirable to restore old values to those registers
  981.    after saving the contents of interest, and then call
  982.    this function using the saved values.
  983.    struct_return is non-zero when the function in question is
  984.    using the structure return conventions on the machine in question;
  985.    0 when it is using the value returning conventions (this often
  986.    means returning pointer to where structure is vs. returning value). */
  987.  
  988. value
  989. value_being_returned (valtype, retbuf, struct_return)
  990.      register struct type *valtype;
  991.      char retbuf[REGISTER_BYTES];
  992.      int struct_return;
  993. {
  994.   register value val;
  995.  
  996.   if (struct_return)
  997.     return value_at (valtype, EXTRACT_STRUCT_VALUE_ADDRESS (retbuf));
  998.  
  999.   val = allocate_value (valtype);
  1000.   EXTRACT_RETURN_VALUE (valtype, retbuf, VALUE_CONTENTS (val));
  1001.  
  1002.   return val;
  1003. }
  1004.  
  1005. /* Return true if the function specified is using the structure returning
  1006.    convention on this machine to return arguments, or 0 if it is using
  1007.    the value returning convention.  FUNCTION is the value representing
  1008.    the function, FUNCADDR is the address of the function, and VALUE_TYPE
  1009.    is the type returned by the function */
  1010.  
  1011. struct block *block_for_pc ();
  1012.  
  1013. int
  1014. using_struct_return (function, funcaddr, value_type)
  1015.      value function;
  1016.      CORE_ADDR funcaddr;
  1017.      struct type *value_type;
  1018. {
  1019.   register enum type_code code = TYPE_CODE (value_type);
  1020.  
  1021.   if (code == TYPE_CODE_STRUCT ||
  1022.       code == TYPE_CODE_UNION ||
  1023.       code == TYPE_CODE_ARRAY)
  1024.     {
  1025.       struct block *b = block_for_pc (funcaddr);
  1026.  
  1027.       if (!(BLOCK_GCC_COMPILED (b) && TYPE_LENGTH (value_type) < 8))
  1028.     return 1;
  1029.     }
  1030.  
  1031.   return 0;
  1032. }
  1033.  
  1034. /* Store VAL so it will be returned if a function returns now.
  1035.    Does not verify that VAL's type matches what the current
  1036.    function wants to return.  */
  1037.  
  1038. void
  1039. set_return_value (val)
  1040.      value val;
  1041. {
  1042.   register enum type_code code = TYPE_CODE (VALUE_TYPE (val));
  1043.   char regbuf[REGISTER_BYTES];
  1044.   double dbuf;
  1045.   LONGEST lbuf;
  1046.  
  1047.   if (code == TYPE_CODE_STRUCT
  1048.       || code == TYPE_CODE_UNION)
  1049.     error ("Specifying a struct or union return value is not supported.");
  1050.  
  1051.   if (code == TYPE_CODE_FLT)
  1052.     {
  1053.       dbuf = value_as_double (val);
  1054.  
  1055.       STORE_RETURN_VALUE (VALUE_TYPE (val), &dbuf);
  1056.     }
  1057.   else
  1058.     {
  1059.       lbuf = value_as_long (val);
  1060.       STORE_RETURN_VALUE (VALUE_TYPE (val), &lbuf);
  1061.     }
  1062. }
  1063.  
  1064. void
  1065. _initialize_values ()
  1066. {
  1067.   add_info ("convenience", convenience_info,
  1068.         "Debugger convenience (\"$foo\") variables.\n\
  1069. These variables are created when you assign them values;\n\
  1070. thus, \"print $foo=1\" gives \"$foo\" the value 1.  Values may be any type.\n\n\
  1071. A few convenience variables are given values automatically GDB:\n\
  1072. \"$_\"holds the last address examined with \"x\" or \"info lines\",\n\
  1073. \"$__\" holds the contents of the last address examined with \"x\".");
  1074.  
  1075.   add_info ("values", value_history_info,
  1076.         "Elements of value history (around item number IDX, or last ten).");
  1077.   add_info_alias ("history", value_history_info, 0);
  1078. }
  1079. @
  1080.  
  1081.  
  1082. 1.1
  1083. log
  1084. @Initial revision
  1085. @
  1086. text
  1087. @d493 6
  1088. d507 8
  1089. d589 8
  1090. @
  1091.